(function() {
	var skill = {
		save: function() {
			db.update(db_table_name, this._id, this);
		},
		destroy: function() {
			//移除商品
			this.content.forEach(function(goods_id) {
				var goods = GoodsCon.get(goods_id);
				goods._setClassify(null);
			});
			//移除子集
			this.childClassifys.forEach(function(child_classify_id) {
				var child_classify = ClassifyCon.get(child_classify);
				child_classify && child_classify.destroy();
			});
			//移除父级
			this.setParentClassify(null);

			db.remove(db_table_name, this._id);
		},
		setInfo: function(info) {
			if (this.classify_name !== info.classify_name) {
				//分类名称
				if (!info.classify_name) {
					throwCusError("分类名命名不可为空")
				}
				if (!helper.folder_name(info.classify_name)) {
					throwCusError("分类名命名不规范")
				}
				this.classify_name = info.classify_name;
			}
			//分类图标
			this.icon_url = info.icon_url || "";

			this._setBuffInfo(info);

			this.save();
		},
		_setBuffInfo: function(info) {
			//打折类型
			var type = +info.type;
			if (type && ClassifyCon.TYPE.hasOwnValue(type)) {
				// throwCusError("找不到指定折扣类型")
				this.type = type;
				//类型对应的相关配置
				//打折：折扣比例，是否有相关梯度
				//满减：满多少，减多少，是否有相关梯度
				//抵用：可抵用的现金额度，是否有相关梯度
				var config = info.config || {};
				this.config = config;
			} else {
				this.type = ClassifyCon.TYPE.无;
				this.config = {};
			}
			console.log(type, ClassifyCon.TYPE, ClassifyCon.TYPE.hasOwnValue(type), this, info.card_factory_config_list);

			var card_factory_config_list = this.card_factory_config_list = [];
			var bus;
			if (Array.isArray(info.card_factory_config_list) && (bus = BusCon.get(this.bus_id))) { //不同会员卡特定的配置
				info.card_factory_config_list.forEach(function(card_factory_config_info) {
					console.log(card_factory_config_info.card_factory_id)
					if (card_factory_config_info && CardFactoryCon.get(card_factory_config_info.card_factory_id)) {
						var result = {
							card_factory_id: card_factory_config_info.card_factory_id,
							enable_buff: !!helper.boolean_parse(card_factory_config_info.enable_buff)
						};
						//打折类型
						var type = +card_factory_config_info.type;
						if (type && ClassifyCon.TYPE.hasOwnValue(type)) {
							// throwCusError("找不到指定折扣类型")
							result.type = type;
							//类型对应的相关配置
							//打折：折扣比例，是否有相关梯度
							//满减：满多少，减多少，是否有相关梯度
							//抵用：可抵用的现金额度，是否有相关梯度
							var config = card_factory_config_info.config || {};
							result.config = config;
						} else {
							result.type = ClassifyCon.TYPE.无;
							result.config = {};
						}
						card_factory_config_list.push(result);
					}
				});
			}
			this.save();
		},
		_getAllContent: function() {
			return this.content || (this.content = []);
		},
		_getContent: function(options) {
			var content = this._getAllContent();
			if (!(options && options.show_un_sold_goods)) {
				var bus = BusCon.get(this.bus_id);
				content = bus._filter_un_sold_goods(content);
			}
			return content;
		},
		_addContent: function(goods_id) {
			var content = this._getAllContent();
			if (content.indexOf(goods_id) === -1) {
				content.push(goods_id);
				this.save();
			}
			return content;
		},
		//获取分类详情
		getGoods: function(num, page, options) {
			var content = this._getContent(options);
			var goods_list = [];
			content = data_cut.page(content, num, page);
			content.forEach(function(goods_id, index) {
				var goods = GoodsCon.get(goods_id);
				if (goods) {
					goods_list.push(goods);
				}
			});
			if (goods_list.length !== content.length) {
				this.content = goods_list.map(function(goods) {
					return goods._id
				});
				this.save();
			}
			return goods_list;
		},
		getGoods_PageInfo: function(num, page, options) {
			var total_num = this._getContent(options).length;
			var result = this.getGoods(num, page);
			return {
				total_num: total_num,
				total_page: Math.round(total_num / result.length) || 1, //至少一页
				goods: result,
				page: +page || 0,
				num: +num || 0
			}
		},
		//非安全，添加商品
		_addGoods: function(goods_id) {
			var goods = GoodsCon.get(goods_id);
			if (!goods) {
				throwCusError("找不到指定商品");
			}
			this._addContent(goods_id);
			goods._addBuff(this.buff_id);
			this.save();
		},
		//非安全，移除分类中的商品
		_removeGoods: function(goods_id) {
			var content = this._getAllContent();
			var index = content.indexOf(goods_id);
			if (index === -1) {
				throwCusError("商品不属于指定分类")
			}
			var goods = GoodsCon.get(goods_id);
			goods._removeBuff(this.buff_id);
			content.splice(index, 1);
			this.save();
		},
		//代码片段，添加子分类
		_addChildClassify: function(child_classify_id) {
			this.childClassifys.push(child_classify_id);
			this.save();
		},
		//代码片段，移除子分类
		_removeChildClassify: function(child_classify_id) {
			this.childClassifys = $Array.remove(this.childClassifys, child_classify_id);
			this.save();
		},
		rename: function(new_classify_name) {
			if (!helper.folder_name(new_classify_name)) {
				throwCusError("分类名命名不规范");
			}
			this.classify_name = new_classify_name;
			this.save();
		},
		setParentClassify: function(parent_classify_id) {
			if (this.parent_classify_id) {
				var parentClassify = ClassifyCon.get(this.parent_classify_id);
				parentClassify && parentClassify._removeChildClassify(this._id);
			}
			if (parent_classify_id) {
				var parentClassify = ClassifyCon.get(parent_classify_id);
				if (!parentClassify) {
					throwCusError("找不到指定父级分类")
				}
				parentClassify._addChildClassify(this._id);
				this.parent_classify_id = parent_classify_id;
			} else {
				this.parent_classify_id = null;
			}
			this.save();
			return parentClassify;
		},
		// 非安全，启用优惠
		_enableBuff: function(info) {
			this.enable_buff = true;
			var buff_id = this.buff_id;
			if (info) { //顺便配置一下优惠配置
				this._setBuffInfo(info);
			}
			if (typeof buff_id !== "string") {
				buff_id = this.buff_id = new BuffCon({
					name: this.classify_name,
					code_id: "类目文件夹",
					config: {
						classify_id: this._id
					}
				})._id;
			}
			this._getAllContent().forEach(function(goods_id) {
				var goods = GoodsCon.get(goods_id);
				goods && goods._addBuff(buff_id)
			});
			this.save();
		},
		// 非安全，禁用优惠
		_disableBuff: function() {
			this.enable_buff = false;
			var buff_id = this.buff_id;
			buff_id && this._getAllContent().forEach(function(goods_id) {
				var goods = GoodsCon.get(goods_id);
				goods && goods._removeBuff(buff_id)
			});
			this.save();
		},
		//获取可以在BUFF中使用的配置
		getBuffEnableInfo: function() {
			var result = this;
			if (!result.enable_buff) {
				return;
			}
			if (!result.type /*result.type == ClassifyCon.TYPE.无*/ ) { //没有配置的话，寻找父级的配置
				while (result.parent_classify_id && (result = ClassifyCon.get(result.parent_classify_id))) {
					//找到一个开启BUFF状态且有BUFF配置的的父级
					if (result.enable_buff && result.type) {
						break;
					}
				}
				if (!result.type) {
					return;
				}
			}
			return result;
		},
		//获取BUFF优惠卡专属配置
		getBuffEnableInfoByCardFactoryId: function(card_factory_id) {
			var classify = this;
			do {
				if (!classify.enable_buff) {
					return;
				}
				var card_factory_config_list = classify.card_factory_config_list || [];
				// console.log(card_factory_id, card_factory_config_list, card_factory_id);
				var card_factory_config_info;
				card_factory_config_list.some(function(_card_factory_config_info) {
					return (_card_factory_config_info.card_factory_id === card_factory_id && (card_factory_config_info = _card_factory_config_info))
				})[0];
				if (card_factory_config_info /*有特定配置*/ || !((classify.parent_classify_id && (classify = ClassifyCon.get(classify.parent_classify_id))) /*已经没有父级*/ )) {
					if (card_factory_config_info && !card_factory_config_info.enable_buff /*如果没有启用*/ ) {
						card_factory_config_info = null;
					}
					// console.log(card_factory_config_info);
					break;
				}
			} while (true);
			return card_factory_config_info;
		},
		//改变商品的位置
		updateGoodsIndex: function(goods_id, goods_index, options) {
			var full_content = this._getAllContent();
			// 根据options，校准index的在完整content中的位置
			if (options) {
				var content = this._getContent(options);
				var _before_goods_id = content[goods_index];
				if (_before_goods_id) {
					goods_index = full_content.indexOf(_before_goods_id);
				}
			}

			var index = full_content.indexOf(goods_id);
			if (index === -1) {
				throwCusError("商品不属于指定商家")
			}
			full_content.splice(index, 1);
			goods_index = ~~goods_index;
			if (goods_index > full_content.length) {
				full_content.push(goods_id);
			} else if (goods_index < 0) {
				full_content.unshift(goods_id);
			} else {
				full_content.splice(goods_index, 0, goods_id)
			}
			this.content = full_content;
			this.save();
		}
	}
	module.exports = skill;

	var db = require("../db/index");
	var UserCon = require("./_con");
	var CardFactoryCon = require("../card_factory/_con");
	var BusCon = require("../bus/_con");
	var GoodsCon = require("../goods/_con");
	var AchCon = require("../ach/_con");
	var PayAchCon = require("../pay_ach/_con");
	var CommentCon = require("../comment/_con");
	var EvaluationCon = require("../eval/_con");
	var ClassifyCon = require("./_con");
	var BuffCon = require("../buff/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var $Array = require("../_include/$.Array");
	var $Object = require("../_include/$.Object");
	var helper = require("../_include/helper");
	var db_table_name = config.dbname.classify;
	var db_id_prefix = config.dbIdPrefix.classify;

}());